﻿// <copyright file="LandUnitImporter.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication.Import
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    internal class LandUnitImporter : BaseImporter<LandUnit>
    {
        private readonly int Visibility = 0;
        private readonly int Name = 1;
        private readonly int Type = 2;
        private readonly int Army = 3;
        private readonly int Active = 4;
        private readonly int Damage = 5;
        private readonly int Experience = 6;
        private readonly int Transport = 7;
        private readonly int PublicInformation = 8;
        private readonly int OwnerInformation = 9;
        private readonly int GMInformation = 10;

        private IEnumerable<BaseLandUnit> baseLandUnits;
        private IEnumerable<Army> armies;
        private IEnumerable<SeaUnit> seaUnits;

        public LandUnitImporter(DataWorkspace workspace)
            : base(workspace)
        {
        }

        protected override void FetchRequisites()
        {
            this.baseLandUnits = this.workspace.ApplicationData.BaseLandUnits.GetQuery().Execute();
            this.armies = this.workspace.ApplicationData.Armies.GetQuery().Execute();
            this.seaUnits = this.workspace.ApplicationData.SeaUnits.GetQuery().Execute();
        }

        protected override bool SkipEntity(int index)
        {
            return false;
        }

        protected override int?[] MapHeaders(string[] stringHeaders)
        {
            int?[] headers = new int?[11];

            for (int i = 0; i < stringHeaders.Length; i++)
            {
                if (stringHeaders[i].StartsWith("Vis", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Visibility] = i;
                }
                else if (stringHeaders[i].StartsWith("Nam", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Name] = i;
                }
                else if (stringHeaders[i].StartsWith("Typ", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Type] = i;
                }
                else if (stringHeaders[i].StartsWith("Arm", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Army] = i;
                }
                else if (stringHeaders[i].StartsWith("Act", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Active] = i;
                }
                else if (stringHeaders[i].StartsWith("Dam", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Damage] = i;
                }
                else if (stringHeaders[i].StartsWith("Exp", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Experience] = i;
                }
                else if (stringHeaders[i].StartsWith("Tran", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Transport] = i;
                }
                else if (stringHeaders[i].StartsWith("Public", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[PublicInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("Owner", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[OwnerInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("GM", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[GMInformation] = i;
                }
            }

            return headers;
        }

        protected override void FillEntity(LandUnit entity, int index, int?[] headers, string[] data)
        {
            if (headers[Visibility].HasValue)
            {
                int value;
                if (int.TryParse(data[index + headers[Visibility].Value], out value))
                {
                    entity.Visibility = value;
                }
            }

            if (headers[Name].HasValue)
            {
                entity.Name = data[index + headers[Name].Value];
            }

            if (headers[Type].HasValue)
            {
                entity.BaseLandUnit = baseLandUnits
                    .Where(e => e.Name.Equals(data[index + headers[Type].Value], StringComparison.InvariantCultureIgnoreCase))
                    .FirstOrDefault();
            }

            if (headers[Army].HasValue)
            {
                entity.Army = armies
                    .Where(e => e.Name.Equals(data[index + headers[Type].Value], StringComparison.InvariantCultureIgnoreCase))
                    .FirstOrDefault();
            }

            if (headers[Active].HasValue)
            {
                bool value;
                if (bool.TryParse(data[index + headers[Active].Value], out value))
                {
                    entity.IsActive = value;
                }
            }

            if (headers[Damage].HasValue)
            {
                double value;
                if (double.TryParse(data[index + headers[Damage].Value], out value))
                {
                    entity.Damage = value;
                }
            }

            if (headers[Experience].HasValue)
            {
                double value;
                if (double.TryParse(data[index + headers[Experience].Value], out value))
                {
                    entity.Experience = value;
                }
            }

            if (headers[Transport].HasValue)
            {
                entity.Transport = seaUnits
                    .Where(e => e.Name.Equals(data[index + headers[Type].Value], StringComparison.InvariantCultureIgnoreCase) &&
                                e.Army.Name == entity.Army.Name)
                    .FirstOrDefault();
            }

            if (headers[PublicInformation].HasValue)
            {
                entity.PublicInformation = data[index + headers[PublicInformation].Value];
            }

            if (headers[OwnerInformation].HasValue)
            {
                entity.OwnerInformation = data[index + headers[OwnerInformation].Value];
            }

            if (headers[GMInformation].HasValue)
            {
                entity.GMInformation = data[index + headers[GMInformation].Value];
            }
        }
    }
}
